package com.liuxing.javaSpringBoot.modules.account.controller;

import com.github.pagehelper.PageInfo;
import com.liuxing.javaSpringBoot.modules.account.entity.Resource;
import com.liuxing.javaSpringBoot.modules.account.service.ResourceService;
import com.liuxing.javaSpringBoot.modules.common.entity.Result;
import com.liuxing.javaSpringBoot.modules.common.entity.SearchBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * @File ResourceController.java
 * @Auth liuxing
 * @Date 2021/8/23 18:59
 * @Email liuxing997@foxmail.com
 */
@RestController
@RequestMapping("/api")
public class ResourceController {

    @Autowired
    private ResourceService resourceService;

    /**
     * 插入资源信息
     * 127.0.0.1/api/resource --- post
     * {"permit":"/api/user"}
     */
    @PostMapping(value = "/resource",consumes = "application/json")
    public Result<Resource> insertResource(@RequestBody Resource resource) {
        return resourceService.insertResource(resource);
    }

    /**
     * 根据资源ID修改资源信息
     * 127.0.0.1/api/resource --- put
     * {"id":2,"permit":"/api/role"}
     */
    @PutMapping(value = "/resource", consumes = "application/json")
    public Result<Resource> updateResourceById(@RequestBody Resource resource) {
        return resourceService.updateResourceById(resource);
    }

    /**
     * 根据资源ID删除资源信息
     * 127.0.0.1/api/resource/3 --- delete
     */
    @DeleteMapping(value = "/resource/{id}")
    public Result<Object> deleteResourceByResourceId(@PathVariable int id) {
        return resourceService.deleteResourceByResourceId(id);
    }

    /**
     * 根据资源ID查询资源信息
     * 127.0.0.1/api/resource/2 --- get
     */
    @GetMapping("/resource/{id}")
    public Resource getResourceByResourceId(@PathVariable int id) {
        return resourceService.getResourceByResourceId(id);
    }

    /**
     * 查询所有资源信息
     * 127.0.0.1/api/resources --- get
     */
    @GetMapping("/resources")
    public List<Resource> getAllResource() {
        return resourceService.getAllResource();
    }

    /**
     * 批量插入资源信息
     * 127.0.0.1/api/batch/resources  --- post
     * [{"permit":"/api/resource"},{"permit":"/api/batch/resources"}]
     */
    @PostMapping(value = "/batch/resources",consumes = "application/json")
    public Result<Object> insertResources(@RequestBody List<Resource> resources) {
        return resourceService.insertResources(resources);
    }

    /**
     * 分页查询资源信息
     * 127.0.0.1/api/resources --- post
     * {"currentPage":1,"pageSize":3,"orderBy":"permit","direction":"desc"}
     */
    @PostMapping(value = "/resources",consumes = "application/json")
    public PageInfo<Resource> getResourcesBySearchBean(@RequestBody SearchBean searchBean) {
        return resourceService.getResourcesBySearchBean(searchBean);
    }

}
